/***************************************************************************************
Firm-embedded productivity and cross-country income differences
Alviarez, Cravino and Ramondo
Journal of Political Economy (2022)

Program: figure_05_phi_domestic_vs_foreign.do
Date: October 2022

Description: Reproduces Figure 5: Aggregate firm-embedded productivity: domestic vs foreign firms.

*****************************************************************************************/

*-------------------------------------------------------------------------------
global typeden=1
include "set_directories.do"
set memory 64g
global lf "LF"

*Log
cap log close
log using "${clogs}/figure_05_phi_domestic_vs_foreign.log",replace
*-------------------------------------------------------------------------------



*-----------------------------------------------------------------
clear all
use "${data}/estimates_sec_naics_sales_s1_base_woparent.dta", clear

local ctryref FR
local grvar0 "gdp_pw_ppp" 
local grvar1 ""	
local rr ""
local var3 "A"
local yy "2016"
merge m:1 year isocode type using "${data}/aggregates_tfp_lp_klems_agg.dta", keepusing(*ctfp* *gdp_pw_ppp*) 
drop if _merge==2
drop _merge
rename DA D_A

foreach var in D_A  {
gen `var'_phi=(-1)*`var'*(0.2)
}
*Drop Mexico and Korea due to MNCs data availability
drop if isocode=="MX" | isocode=="KR" 

tempfile temp
save `temp', replace


*Construct the market shares, Snn and Sfn, from MNC data. Snn=Xnn/Xn 
*--------------------------------------------------
clear all
use isocode sector1 sector year GO_usd_mnc GO_usd_nt using "${data}/klems_oecd_unido_orbis_sales_emp_exp.dta", clear

drop if sector1==sector
keep if year==`yy'
keep if isocode=="DK" | isocode=="JP" | isocode=="IT" | isocode=="KR" | isocode=="DE" | isocode=="FR" | isocode=="ES" |  isocode=="MX" |  isocode=="GB" |  isocode=="PL" |  isocode=="GR" |  isocode=="NL" |  isocode=="RO" |  isocode=="BE" |  isocode=="AT" |  isocode=="PT" |  isocode=="FI" |  isocode=="SE" |  isocode=="CZ" |  isocode=="SK" |  isocode=="BG" |  isocode=="HU" |  isocode=="HR" |  isocode=="LV" |  isocode=="SI" |  isocode=="LT" |  isocode=="EE" 
drop if sector=="Public_Administration (O)"


gen Sfn=(GO_usd_mnc)/GO_usd_nt
sum Sfn, d
replace Sfn=. if Sfn>1 | Sfn<=0
gen Snn=1-Sfn
gsort sector1 sector -Snn

gen ln_Snn=ln(Snn)
gen a=ln_Snn if isocode=="`ctryref'" 
by sector1 sector year, sort: egen b=max(a)
gen delta_Snn=ln_Snn-b
drop a b ln_Snn

gen ln_Sfn=ln(Sfn)
gen a=ln_Sfn if isocode=="`ctryref'" 
by sector1 sector year, sort: egen b=max(a)
gen delta_Sfn=ln_Sfn-b
drop a b ln_Sfn

tempfile temp0
save `temp0', replace


*Save Srr (in equation (28) in the paper), this is the market share of domestic firms in France for the reference country (FR) 
*--------------------------------------------------
use `temp0', clear
keep if isocode=="`ctryref'" & year==`yy'
drop isocode
tempfile temp1
save `temp1', replace


*Bring the sectoral weights (theta_nj)
*--------------------------------------------------
use "${data}/sector_weights.dta", clear
fillin isocode sector year
keep if year==`yy'
foreach v in theta theta_fr theta_mso theta_mso_fr {
replace `v'=0 if `v'==.
}
tempfile weights
save `weights', replace



*Bring the sectoral weights and Snn, Sfn and Srr to the dataset, then renormalize theta whenever necessary according with the sectoral information availble for each country
*--------------------------------------------------
use `temp', clear
merge m:1 year isocode type sector  using `weights', keepusing(theta*)
drop if _merge==2
drop _merge

merge m:1 sector1 sector isocode year using `temp0', keepusing(delta_Snn delta_Sfn) 
drop if _merge==2
drop _merge
sum D_A
sum year if D_A==.
tab isocode sector1 if delta_Snn==. | delta_Sfn==.
replace D_A=. if delta_Snn==. | delta_Sfn==.


*Renormalize theta
*--------------------------------------------------
foreach j in D_A {
foreach i in theta theta_fr {
display "`i'"
display "`j'"
gen ii=`i' if `j'!=. 
by table isocode year, sort: egen xx=total(ii) 
gen `i'_`j'=ii/xx
replace `i'_`j'=0 if `i'_`j'==.
drop ii xx 
}
}
*
tempfile weights_renormalized
save `weights_renormalized', replace


*Merge all the necessary variables 
*-----------------------------------
clear all
use `temp', clear
merge m:1 table isocode year sector using  `weights_renormalized', keepusing(theta*D_A)
drop _merge
rename theta_D_A theta
rename theta_fr_D_A theta_`ctryref'

merge m:1 sector1 sector isocode year using `temp0', keepusing(delta_Snn delta_Sfn) 
drop if _merge==2
drop _merge

merge m:1 year sector using `temp1', keepusing(Snn Sfn) 
drop if _merge==2
drop _merge
rename Snn Snn_`ctryref'
rename Sfn Sfn_`ctryref'

gen phi_n_j=(-1)*D_A*(0.2)
gen phi_nn_j=((delta_Snn)*(0.2))+phi_n_j
gen phi_fn_j=((delta_Sfn)*(0.2))+phi_n_j

gen phi_n_j_theta=phi_n_j*theta
gen phi_nn_j_theta=phi_nn_j*theta*(Snn_`ctryref')
gen phi_fn_j_theta=phi_fn_j*theta*(Sfn_`ctryref')

gen phi_n_j_theta_`ctryref'=phi_n_j*theta_`ctryref'
gen phi_nn_j_theta_`ctryref'=phi_nn_j*theta_`ctryref'*(Snn_`ctryref')
gen phi_fn_j_theta_`ctryref'=phi_fn_j*theta_`ctryref'*(Sfn_`ctryref')

collapse (sum) phi_nn_agg=phi_nn_j_theta phi_fn_agg=phi_fn_j_theta  phi_n_agg=phi_n_j_theta phi_nn_agg_`ctryref'=phi_nn_j_theta_`ctryref' phi_fn_agg_`ctryref'=phi_fn_j_theta_`ctryref' phi_n_agg_`ctryref'=phi_n_j_theta_`ctryref' (mean) Snn* Sfn* ln_`grvar0', by(isocode year)

				
tempfile figure_foreign_domestic
save `figure_foreign_domestic', replace



*---------------------------------------------------------
display "Figure 5: Aggregate firm-embedded productivity: domestic vs foreign firms"
*---------------------------------------------------------
use `figure_foreign_domestic', clear

global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(maroon)  msize(medium) mlabsize(vsmall)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(green) msize(medium) mlabsize(vsmall)
global opt3 msymbol(none) mlabcolor(none) 

gen phi_nn_agg_w=phi_nn_agg
gen phi_fn_agg_w=phi_fn_agg
gen phi_n_agg_sq=phi_nn_agg_w+phi_fn_agg_w

local LHS1 phi_nn_agg_w
local LHS2 phi_fn_agg_w
local RHS phi_n_agg_sq
sum `LHS1' `LHS2' `RHS' 
local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:nn}, {&Delta}`=ustrunescape("\u03D5\u0303")'{sub:Fn}"	
local xtitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}"
local firm_emb_xnn "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:nn}" 
local firm_emb_xfn "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:Fn}" 


reg `LHS1' `RHS' 
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.01)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.01)

reg `LHS2' `RHS' 
mat b = e(b)
mat V = e(V)
global c2: display %-03.2fc round(b[1,1],0.01)
global se2: display %-03.2fc round(sqrt(V[1,1]),0.01)

global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(maroon)  msize(medium) mlabsize(vsmall)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(green) msize(medium) mlabsize(vsmall)
global opt3 msymbol(none) mlabcolor(none) 
two (scatter `LHS1' `RHS', sort $opt1 mcolor(maroon)) (lfit `LHS1' `RHS', lcolor(maroon)) (line `LHS1' `RHS', lcolor(none))  ///
(scatter `LHS2' `RHS', sort $opt2 mcolor(green)) (lfit `LHS2' `RHS', lcolor(green)) (line `LHS2' `RHS', lcolor(none)), ylabel(-0.6(0.2)0.2, format(%5.1f) labsize(medlarge)) xlabel(-0.6(0.2)0, format(%5.1f) labsize(medlarge))  xscale(titlegap(*1)) ///
ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
legend(order(2 "`firm_emb_xnn':  $c1 ($se1)" 5 "`firm_emb_xfn':  $c2 ($se2)") rows(2) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 
graph export "${rmain}/fig_05.pdf", replace


log close 


